package com.prs.crm.action.sale;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import com.prs.crm.domain.sale.ShippingWay;
import com.prs.crm.service.sale.ShippingWayService;

public class ShippingWayAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;

	private ShippingWay shippingWay;

	private Integer[] typeIds;

	private ShippingWayService shippingWayService;

	private PaginationSupport pagination;

	@Action("listShippingWay")
	public String listShippingWay() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ShippingWay.class);
		this.setPagination(this.getShippingWayService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editShippingWay")
	public String editShippingWay() {
		return SUCCESS;
	}

	@Action(value = "saveShippingWay", results = { @Result(name = "success", type = "redirect", location = "listShippingWay"),
			@Result(name="error",type="chain",location="editShippingWay")})
	public String saveShippingWay() {
		if(shippingWay.getName() == null || shippingWay.getName().equals("")) {
			this.addActionError("收货方式名称不能为空");
			return ERROR;
		}
		for(ShippingWay shippingWay1:this.getShippingWayService().getAllShippingWay()){
			if(shippingWay1.getName().trim().equals(shippingWay.getName().trim())){
				this.addActionError("收货方式名称已经存在");
				return ERROR;
			}
		}
		this.getShippingWayService().save(shippingWay);
		return SUCCESS;
	}

	@Action(value = "removeShippingWay", results = { @Result(name = "success", type = "redirect", location = "listShippingWay") })
	public String removeShippingWay() {
		this.getShippingWayService().batchRemove(typeIds);
		return SUCCESS;
	}

	public ShippingWay getShippingWay() {
		return shippingWay;
	}

	public void setShippingWay(ShippingWay shippingWay) {
		this.shippingWay = shippingWay;
	}

	public Integer[] getTypeIds() {
		return typeIds;
	}

	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}

	public ShippingWayService getShippingWayService() {
		return shippingWayService;
	}

	public void setShippingWayService(ShippingWayService shippingWayService) {
		this.shippingWayService = shippingWayService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

}
